package org.droid.campusorder.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.droid.R;
import org.droid.campusorder.OrderActivity;
import org.droid.campusorder.common.Debug;

import android.content.Context;
import android.graphics.Color;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class OrderListViewAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private Holder holder;
	private ArrayList<HashMap<String, String>> list;
	private Context context;
	private String count[]; // 菜的份数
	private Debug debug;
	private boolean countIsZero[];  //用于判断份数是否为0
	public TextView[] countTextViewsCanche;
	public TextView[] dishnameTextViewsCanche;
	ImageButton[] mAddIbtnsCanche;
	ImageButton[] mMinusIbtnsCanche;

	public OrderListViewAdapter(Context context) {
		super();
		this.context = context;
		inflater = LayoutInflater.from(context);
		debug = new Debug("OrderListViewAdapter");
	}

	public void setList(ArrayList<HashMap<String, String>> list) {
		count = new String[list.size()];
		countIsZero = new boolean[list.size()]; //默认为false
		countTextViewsCanche = new TextView[list.size()];
		mAddIbtnsCanche = new ImageButton[list.size()];
		mMinusIbtnsCanche = new ImageButton[list.size()];
		dishnameTextViewsCanche = new TextView[list.size()];
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public HashMap<String, String> getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int positiontTemp = position;
		// TODO Auto-generated method stub
		count[positiontTemp] = (getItem(position)).get("countnum").trim();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.order_lv_item, null);
			holder = new Holder();
			holder.tv_orderlv_dishprice = (TextView) convertView
					.findViewById(R.id.tv_orderlv_dishprice);
			holder.tv_orderlv_dishname = (TextView) convertView
					.findViewById(R.id.tv_orderlv_dishname);
			holder.tv_orderlv_countnum = (TextView) convertView
					.findViewById(R.id.tv_orderlv_countnum);
			holder.ibtn_minus = (ImageButton) convertView
					.findViewById(R.id.ibtn_orderlv_minus);
			holder.ibtn_add = (ImageButton) convertView
					.findViewById(R.id.ibtn_orderlv_add);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		countTextViewsCanche[positiontTemp] = holder.tv_orderlv_countnum;
		mAddIbtnsCanche[positiontTemp] = holder.ibtn_add;
		mMinusIbtnsCanche[positiontTemp] = holder.ibtn_add;
		dishnameTextViewsCanche[positiontTemp] = holder.tv_orderlv_dishname;

		holder.tv_orderlv_dishname.setText((getItem(position)).get("dishname")
				.trim());
		holder.tv_orderlv_dishprice.setText((getItem(position))
				.get("dishprice").trim());
		holder.tv_orderlv_countnum.setText(count[positiontTemp]);

		holder.ibtn_minus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(context, "减一下", Toast.LENGTH_LONG).show();
				int tempcount = Integer.parseInt(count[positiontTemp]) - 1;
				debug.debug(tempcount);
				holder.ibtn_add = mAddIbtnsCanche[positiontTemp];
				holder.ibtn_minus = mMinusIbtnsCanche[positiontTemp];
				if (tempcount >= 0) {
					if (holder.ibtn_add.getVisibility() == View.GONE) {
						holder.ibtn_add.setVisibility(View.VISIBLE);
					}
					count[positiontTemp] = String.valueOf(tempcount);
					list.get(positiontTemp).put("countnum",
							count[positiontTemp]);
					holder.tv_orderlv_countnum = countTextViewsCanche[positiontTemp];
					holder.tv_orderlv_countnum.setText((String
							.valueOf(count[positiontTemp])));
					float sumPrice = 0;
					// 计算总价
					for (int i = 0; i < list.size(); i++) {
						String tempPrice = list.get(i).get("dishprice");
						if (i == positiontTemp) {
							sumPrice += Float.parseFloat(tempPrice)
									* Integer.parseInt(count[positiontTemp]);
						} else {
							String tempCount = list.get(i).get("countnum");
							sumPrice += Float.parseFloat(tempPrice)
									* Integer.parseInt(tempCount);
						}
					}
					Message msg =new Message();
					msg.obj = sumPrice;
					OrderActivity.calculateHandler.sendMessage(msg);
					if (tempcount == 0) {
						holder.tv_orderlv_dishname = dishnameTextViewsCanche[positiontTemp];
						String dishnameTemp =holder.tv_orderlv_dishname.getText().toString().trim();
						SpannableString ss = new SpannableString(dishnameTemp);
						ss.setSpan(new StrikethroughSpan(), 0, dishnameTemp.length(),
								Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						 ss.setSpan(new ForegroundColorSpan(Color.RED), 0, dishnameTemp.length(),
					                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						holder.tv_orderlv_dishname.setText(ss);
						countIsZero[positiontTemp]=true; //份数为0
//						v.setVisibility(View.GONE);
//						mMinusIbtnsCanche[positiontTemp] = (ImageButton) v;
					}
				}
			}
		});

		holder.ibtn_add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(context, "加一下", Toast.LENGTH_LONG).show();
				if(countIsZero[positiontTemp])
				{
					holder.tv_orderlv_dishname = dishnameTextViewsCanche[positiontTemp];
					String dishnameTemp =holder.tv_orderlv_dishname.getText().toString().trim();
					SpannableString ss = new SpannableString(dishnameTemp);
					ss.setSpan(new ForegroundColorSpan(Color.WHITE), 0, dishnameTemp.length(),
				                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
					holder.tv_orderlv_dishname.setText(ss);
				}
				int tempcount = Integer.parseInt(count[positiontTemp]) + 1;
				holder.ibtn_add = mAddIbtnsCanche[positiontTemp];
				holder.ibtn_minus = mMinusIbtnsCanche[positiontTemp];
				debug.debug(tempcount);
				if (tempcount <= 10) {
					if (holder.ibtn_minus.getVisibility() == View.GONE) {
						holder.ibtn_minus.setVisibility(View.VISIBLE);
					}
					count[positiontTemp] = String.valueOf(tempcount);
					list.get(positiontTemp).put("countnum",
							count[positiontTemp]);
					holder.tv_orderlv_countnum = countTextViewsCanche[positiontTemp];
					holder.tv_orderlv_countnum.setText((String
							.valueOf(count[positiontTemp])));
					float sumPrice = 0;
					// 计算总价
					for (int i = 0; i < list.size(); i++) {
						String tempPrice = list.get(i).get("dishprice");
						if (i == positiontTemp) {
							sumPrice += Float.parseFloat(tempPrice)
									* Integer.parseInt(count[positiontTemp]);
						} else {
							String tempCount = list.get(i).get("countnum");
							sumPrice += Float.parseFloat(tempPrice)
									* Integer.parseInt(tempCount);
						}
					}
					Message msg =new Message();
					msg.obj = sumPrice;
					OrderActivity.calculateHandler.sendMessage(msg);
					if (tempcount == 10) {
//						v.setVisibility(View.GONE);
//						mAddIbtnsCanche[positiontTemp] = (ImageButton) v;
					}
				}
			}
		});
		return convertView;
	}

	class Holder {
		TextView tv_orderlv_dishprice;
		TextView tv_orderlv_dishname;
		TextView tv_orderlv_countnum;
		ImageButton ibtn_minus;
		ImageButton ibtn_add;
	}
}
